<?php
namespace BeautyParlorAdmin\Controller;

use Think\Controller;
use Think\Page;
use Common\Util\UploadHandler;

class OrderController extends BaseController {
    public function index(){
    	$OrderModel = D('Order');
    	$id = $this->id;
    	
   		$count = $OrderModel->where(array('beauty_id' => $id))->count();
   		$page = new Page($count);
    	$list = $OrderModel->field(array(
	    		'o.*',
	    		'b.name' => 'beautician_name',
	    		'bp.name'	=> 'beauty_name',
    			'u.username' => 'order_user_name',
    			'u.mobile'
    		))
    		->join('o left join '.C('DB_PREFIX').'beautician as b on o.beautician_id=b.id')
    		->join('left join '.C('DB_PREFIX').'beauty_parlor as bp on o.beauty_id=bp.id')
    		->join('left join '.C('DB_PREFIX').'user as u on o.user_id=u.id')
    		->order('created desc')
    		->where(array('o.beauty_id' => $id))
    		->limit($page->firstRow. ','. $page->listRows)
    	->select();
    	$this->assign('list', $list);
    	$this->assign('page', $page->show());
    	$this->display();
    }
    
    public function search() {
    	$orderNumber = I('get.order_number');
    	$mobile = I('get.mobile');
    	
    	$id = $this->id;
    	$where = array('beauty_id' => $id);
    	
    	
    	$OrderModel = D('Order');
    	if ($orderNumber) {
    		$where ['o.order_number'] = $orderNumber;
    	}
    	
    	if ($mobile) {
    		$where ['u.mobile'] = $mobile;
    	}
    	
    	$count = $OrderModel->join('left join '.C('DB_PREFIX').'user as u on o.user_id=u.id')->where($where)->count();
    	$page = new Page($count);
    	 
    	$list = $OrderModel->field(array(
    			'o.*',
    			'b.name' => 'beautician_name',
    			'bp.name'	=> 'beauty_name',
    			'u.username' => 'order_user_name',
    			'u.mobile'
    	))
    	->join('o left join '.C('DB_PREFIX').'beautician as b on o.beautician_id=b.id')
    	->join('left join '.C('DB_PREFIX').'beauty_parlor as bp on o.beauty_id=bp.id')
    	->join('left join '.C('DB_PREFIX').'user as u on o.user_id=u.id')
    	->where($where)
    	->order('created desc')
    	->limit($page->firstRow. ','. $page->listRows)
    	->select();
    	
    	$this->assign('list', $list);
    	$this->assign('page', $page->show());
    	$this->display('index');
    }
    
    public function delete() {
    	
    }
}